*************************************************
*			05_compile							*
*												*
*	Combines 4 datasets for analysis file		*
*												*
*	Raw: 	ags_replication.dta
*
*	Temp: 	netgen_data.dta			 			*
*			facility_capacity_2021.dta			*
*			923_resale.dta						*
*			windiq_repower.dta					*
*
*	Output: analysis_data.dta					*
*												*
*************************************************




*********************
* Read Data and Clean for Wind
**********************


* Prep 1603 Flag from Aldy, Gerarden, Sweeney 
use "${wind}/data_raw/replication/wind_replication.dta" ,clear
keep facilityid flag_1603 
duplicates drop
tempfile rep
save `rep'  

* Use Facility-month net generation data as backbone
use "${wind}/temp/netgen_data.dta", clear

***
* Merge in 1603 flag from AGS replication data
*** 

merge m:1 facilityid using `rep', nogen  keep(1 3)
replace flag_1603 = 0 if missing(flag_1603)
replace flag_1603 = 0 if vintage<2008 


***
*Merge in Prices
***

merge m:1 facilityid year using "${wind}/temp/923_resale.dta", keep(1 3) gen(whls)

bys facilityid: gegen ws_price120 = mean(cond(count==120,av_wholesale_price,.))
bys facilityid: gegen ws_price = mean(cond(whls==3,av_wholesale_price,.))

* Replace non-merged
ren ws_price120  av_price
replace av_price = ws_price if av_price==.

* Replace with average in state if missing
bys state: gegen ws_price_st = mean(av_wholesale_price)
replace av_price = ws_price_st if av_price==.

sum av_price if count==120, detail
gen high_price = av_price >  `r(p50)'


***
* Add Repowering from Wind IQ (Proprietary)
***

merge m:1 facilityid using "${wind}/temp/windiq_repower.dta" ,  keepusing(repower) keep(1 3)
gen any_repower = repower_flag==1 & _m==3
drop _m

*drop propriatary data for published data
drop repower_flag

***
* Merge in Capacity
***
merge m:1 facilityid using "${wind}/temp/facility_capacity_2021.dta", keep(1 3)
count if _m==1 & count==1 		// drops 
drop if _m==1
drop _m


* Generate Monthly capacity  factor
gen 	days = 31 if inlist(month, 1,3,5,7,8,10,12)
replace days = 30 if inlist(month, 4,6,9,11)
replace days = 28 if month == 2 & mod(year,4) != 0
replace days = 29 if month == 2 & (mod(year,4) == 0 & year!=2000)

gen total_capacity = 24*days*nameplatecapacitymw
gen capacity_factor = 100*netgen/total_capacity

* Top and Bottom Code 
replace capacity_factor=100 if capacity_factor > 100
replace capacity_factor=0 if capacity_factor <0


save "${wind}/data_final/analysis_data.dta", replace







